import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;

public class Demo {

    /**
     * Tworzenie, update i niszczenie okna głownego programu
     */
    public void start() {
        try {
            Display.setDisplayMode(new DisplayMode(800, 600));
            Display.create();
        } catch (LWJGLException e) {
            e.printStackTrace();
        }

        //inicjalizacja openGL
        GL11.glMatrixMode(GL11.GL_PROJECTION);
        GL11.glLoadIdentity();
        GL11.glOrtho(0, 800, 0, 600, 1, -1);
        GL11.glMatrixMode(GL11.GL_MODELVIEW);

        while (!Display.isCloseRequested()) {
            pollInput();
            drawQuad();
            Display.update();
        }

        Display.destroy();
    }

    /**
     * Rysowanie kwadratu
     */
    private void drawQuad() {
        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT|GL11.GL_DEPTH_BUFFER_BIT);
        GL11.glColor3f(0.3f, 0.8f, 0.5f);
        GL11.glBegin(GL11.GL_QUADS);
            GL11.glVertex2d(100, 100);
            GL11.glVertex2d(100+200, 100);
            GL11.glVertex2d(100+200, 100+200);
            GL11.glVertex2d(100, 100+200);
        GL11.glEnd();
    }

    private void pollInput() {
        if (Mouse.isButtonDown(0)) {
            int x = Mouse.getX();
            int y = Mouse.getY();
            System.out.println("Mysz wcisnieta w X: " + x + " Y: " + y);
        }

        if(Keyboard.isKeyDown(Keyboard.KEY_SPACE)) {
            System.out.println("Spacja wcisnieta!");
        }

        while (Keyboard.next()) {
            if (Keyboard.getEventKeyState()) {
                if (Keyboard.getEventKey() == Keyboard.KEY_A) {
                    System.out.println("A Key Pressed");
                }
                if (Keyboard.getEventKey() == Keyboard.KEY_S) {
                    System.out.println("S Key Pressed");
                }
                if (Keyboard.getEventKey() == Keyboard.KEY_D) {
                    System.out.println("D Key Pressed");
                }
            } else {
                if (Keyboard.getEventKey() == Keyboard.KEY_A) {
                    System.out.println("A Key Released");
                }
                if (Keyboard.getEventKey() == Keyboard.KEY_S) {
                    System.out.println("S Key Released");
                }
                if (Keyboard.getEventKey() == Keyboard.KEY_D) {
                    System.out.println("D Key Released");
                }
            }
        }
    }

    public static void main(String[] args) {
        Demo demo = new Demo();
        demo.start();
    }
}

